Python is a high-level, interpreted, open-source programming language that emphasizes readability of source code. In this notebook some of the features of the language as well as key modules in the the Scientific Python ecosystem will be introduced.

Aside using the python shell, Python scrips, and IPython with the "Hello world" example.

Variable Types


In [1]:
a = 1   # integers

In [2]:
a + 1


Out[2]:
2

In [3]:
b = 2.1  # float
type(b)


Out[3]:
float

In [4]:
c = 1.5 + 0.5j  # complex numbers
print c.real
print c.imag
type(c)


1.5
0.5
Out[4]:
complex

In [5]:
d = 3 > 4  # booleans
print d
type(d)


False
Out[5]:
bool

In [6]:
a = 1
b = 2.5
a + b


Out[6]:
3.5

In [7]:
# types can be cast
a = 1
print a
print float(a)


1
1.0

In [8]:
s = "Hello everyone"   # strings
type(s)


Out[8]:
str

Containers

Lists


In [9]:
# lists
l = ['red', 'blue', 'green', 'black', 'white']
type(l)


Out[9]:
list

In [10]:
len(l)


Out[10]:
5

In [11]:
print l[0]
print l[1]
print l[2]


red
blue
green

In [12]:
print l[-1]   # last element
print l[-2]


white
black

In [13]:
print l[2:5]


['green', 'black', 'white']

In [14]:
print l[2:-1]


['green', 'black']

In [16]:
l


Out[16]:
['red', 'blue', 'green', 'black', 'white']

In [15]:
print l[1:6:2]


['blue', 'black']

In [17]:
l[::-1]


Out[17]:
['white', 'black', 'green', 'blue', 'red']

In [18]:
l


Out[18]:
['red', 'blue', 'green', 'black', 'white']

In [19]:
l[0] = 'orange'
print l


['orange', 'blue', 'green', 'black', 'white']

In [20]:
ll = [5, 22.9, 14.8+1j, 'hello', [1,2,3]]

In [21]:
ll


Out[21]:
[5, 22.9, (14.8+1j), 'hello', [1, 2, 3]]

In [22]:
print ll[0]
print ll[1]
print ll[2]
print ll[3]
print ll[4]


5
22.9
(14.8+1j)
hello
[1, 2, 3]

Dictionaries


In [23]:
d = {'name': 'Jonathan', 'id': 223984, 'location': 'USA'}

In [24]:
d.keys()


Out[24]:
['location', 'name', 'id']

In [25]:
d.values()


Out[25]:
['USA', 'Jonathan', 223984]

In [26]:
d['name']


Out[26]:
'Jonathan'

In [27]:
d['id']


Out[27]:
223984

In [28]:
d['id'] = 1234

In [29]:
d['id']


Out[29]:
1234

Tuples


In [30]:
t = ('red', 'blue', 'green')

In [31]:
t[0]


Out[31]:
'red'

In [32]:
t[1:3]


Out[32]:
('blue', 'green')

In [33]:
t[1] = 'orange'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-fbab64ebbc9d> in <module>()
----> 1 t[1] = 'orange'

TypeError: 'tuple' object does not support item assignment

Flow control


In [34]:
a = 10
if a == 10:
    print "a is 10"
# if, elif, else


a is 10

In [38]:
a = 10
if a > 10:
    print "a is larger than 10"
else:
    print "a is less than 10... or maybe equal too"


a is less than 10... or maybe equal too

In [41]:
a = 4
if a > 10:
    print "a is larger than 10"
elif a < 10:
    print "a is less than 10"
else:
    print "a is equal to 10"


a is less than 10

In [42]:
for i in range(10):
    print i


0
1
2
3
4
5
6
7
8
9

In [43]:
for color in ['red', 'blue', 'orange']:
    print "My favorite color is", color


My favorite color is red
My favorite color is blue
My favorite color is orange

In [44]:
# list comprehensions
[i*2 for i in range(10)]


Out[44]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Functions


In [45]:
def func():
    print "Hello world"

In [46]:
func()


Hello world

In [47]:
def func2(name):
    print "Hello", name

In [48]:
func2("Jonathan")


Hello Jonathan

In [49]:
def times2(x):
    return x * 2

In [50]:
y = times2(2)
print y


4

In [51]:
def times_something(x, y=2):
    print x*y

In [52]:
times_something(3)


6

In [53]:
times_something(3, 3)


9

In [54]:
w = times_something

In [55]:
w(3, 3)


9

In [56]:
f_list = [times_something, times2]

In [57]:
f_list[1](9)


Out[57]:
18

Classes


In [58]:
class Car(object):
    
    engine = 'V4'    # class attribute
    
    def start(self):  # class method
        print "Starting the car with a", self.engine, "engine"

In [ ]:
class SuperCar(Car):

In [59]:
mycar = Car()

In [60]:
type(mycar)


Out[60]:
__main__.Car

In [61]:
mycar.engine


Out[61]:
'V4'

In [64]:
mycar.start()


Starting the car with a V4 engine

In [65]:
mycar.engine = 'V6'

In [66]:
mycar.engine


Out[66]:
'V6'

In [67]:
mycar.start()


Starting the car with a V6 engine

The Scientific Python stack

NumPy


In [68]:
import numpy as np

In [76]:
a = np.array([0, 1, 2, 3, 4, 5, 6, 7])
a

In [71]:
a.shape


Out[71]:
(4,)

In [72]:
a.ndim


Out[72]:
1

In [73]:
a.dtype


Out[73]:
dtype('int64')

In [78]:
a[0::2]


Out[78]:
array([0, 2, 4, 6])

In [80]:
a[a>3]


Out[80]:
array([4, 5, 6, 7])

In [83]:
a * 2 + 100


Out[83]:
array([100, 102, 104, 106, 108, 110, 112, 114])

In [86]:
a.


Out[86]:
3.5

In [87]:
b = np.arange(12).reshape(3,4)

In [88]:
b.shape


Out[88]:
(3, 4)

In [ ]:
b

In [ ]:
b[1,2]

In [ ]:
b[0:2, ::-1]

matplotlib


In [92]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [93]:
plot([1,2,3])


Out[93]:
[<matplotlib.lines.Line2D at 0x1083f0090>]

In [94]:
a = np.random.rand(30, 30)
imshow(a)
colorbar()


Out[94]:
<matplotlib.colorbar.Colorbar instance at 0x1085b6c20>

In [97]:
import matplotlib.pyplot as plt

In [ ]: